package com.schoolcloud.share.controller;

import com.schoolcloud.common.model.PageDTO;
import com.schoolcloud.common.model.R;
import com.schoolcloud.share.dto.SectionVideoDelDTO;
import com.schoolcloud.share.dto.SectionVideoRelatedDTO;
import com.schoolcloud.share.entity.SectionVideos;
import com.schoolcloud.share.entity.SetSections;
import com.schoolcloud.share.query.SetSectionQuery;
import com.schoolcloud.share.service.SectionVideosService;
import com.schoolcloud.share.service.SetSectionsService;
import com.schoolcloud.share.service.VideosService;
import com.schoolcloud.share.vo.SectionVideoItem;
import com.schoolcloud.share.vo.SectionVideoVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/share/set/section")
public class ShareSetSectionController {
    private final SetSectionsService setSectionsService;
    private final SectionVideosService sectionVideosService;
    private final VideosService videosService;

    @GetMapping("/page")
    public R<PageDTO<SectionVideoVO>> getSetSectionsPage(SetSectionQuery query) {
        PageDTO<SectionVideoVO> pageDTO = videosService.getSetSectionsPage(query);
        return R.ok(pageDTO);
    }

    @GetMapping("/related/list/{sectionId}")
    public R<List<SectionVideoItem>> getSetSectionRelatedList(@PathVariable Integer sectionId) {
        List<SectionVideoItem> list = videosService.getSetSectionRelatedList(sectionId);
        return R.ok(list);
    }

    @PostMapping
    public R<SetSections> createSetSection(@RequestBody SetSections setSections) {
        setSectionsService.createSetSection(setSections);
        return R.ok("添加成功！", setSections);
    }

    @PostMapping("/video/related")
    public R<?> createSetSectionRelation(@RequestBody SectionVideoRelatedDTO relations) {
        sectionVideosService.createSectionVideoRelation(relations);
        return R.ok().setMsg("关联成功！");
    }

    @DeleteMapping("/video/related/ids")
    public R<?> deleteSetSectionRelationByIds(@RequestBody List<Integer> relationIds) {
        sectionVideosService.removeByIds(relationIds);
        return R.ok().setMsg("删除成功！");
    }

    @DeleteMapping("/video/related")
    public R<?> deleteSetSectionRelation(@RequestBody List<SectionVideoDelDTO> relation) {
        relation.forEach(item -> {
            sectionVideosService.lambdaUpdate().eq(SectionVideos::getSectionId, item.getSectionId())
                    .eq(SectionVideos::getVideoId, item.getVideoId()).remove();
        });
        return R.ok().setMsg("删除成功！");
    }

    @PutMapping
    public R<SetSections> updateSetSection(@RequestBody SetSections setSections) {
        setSectionsService.updateById(setSections);
        return R.ok("修改成功！", setSections);
    }

    @DeleteMapping("/{id}")
    public R<String> deleteSetSection(@PathVariable Integer id) {
        setSectionsService.removeById(id);
        return R.ok("删除成功！");
    }

    @GetMapping("/{id}")
    public R<SetSections> getSetSectionById(@PathVariable Integer id) {
        return R.ok(setSectionsService.getById(id));
    }
}
